﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ENG.TeachersDiary.Model;
using ENG.TeachersDiary.Forms;
using ENG.TeachersDiary.Classes;

namespace ENG.TeachersDiary
{
  public partial class FrmEditStudentsMarks : Form
  {
    private class MyItem : IComparable<MyItem>
    {
      

      public Model.MarkCode Code;
      public MyItem(Model.MarkCode code)
      {
        this.Code = code;
      }
      public override string ToString()
      {
        return Code.Display;
      }

      #region IComparable<MyItem> Members

      public int CompareTo(MyItem other)
      {
        return this.Code.MarkCodeID.CompareTo(other.Code.MarkCodeID);
      }

      #endregion
    }

    private DiaryDB db;
    private Student currentStudent;

    public FrmEditStudentsMarks()
    {
      InitializeComponent();

      ReDB();      
    }

    public void ReDB()
    {
      db = Common.DBConn;

      var markCodes = db.MarkCodes.OrderBy(i => i.OrderIndex);

      cmbStudent.DataSource =
        db.Students.OrderBy(i => i.Surname).ToList();
      markCodeBindingSource.DataSource = db.MarkCodes.Where(i => i.IsEvaluable);
      markGroupBindingSource.DataSource = db.MarkGroups;
    }

    internal void Init(int? studentID)
    {
      if (studentID.HasValue)
      {
        currentStudent = db.Students.First(i => i.StudentID == studentID.Value);
        cmbStudent.SelectedItem = currentStudent;
      }
    }

    private void cmbStudent_SelectedIndexChanged(object sender, EventArgs e)
    {
      currentStudent = cmbStudent.SelectedItem as Model.Student;

      markBindingSource.DataSource =
        db.Marks.Where(i => i.StudentID == currentStudent.StudentID).OrderBy(i => i.Date);
    }

    private void studentBindingSource_CurrentChanged(object sender, EventArgs e)
    {
      db.SubmitChanges();
      changedStudentsIDs.Add(currentStudent.StudentID);
    }

    private void cmbMark_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (chkAutosave.Checked)
        SaveCurrent();
      else
        btnSave.Enabled = true;
    }

    private void SaveCurrent()
    {
      Model.Mark m = markBindingSource.Current as Model.Mark;
      m.MarkCode = (cmbMark.SelectedItem as Model.MarkCode);
      m.Date = dtpDate.Value;
      m.MarkGroup = (cmbMarkGroup.SelectedItem as Model.MarkGroup);

      db.SubmitChanges();
      changedStudentsIDs.Add(currentStudent.StudentID);
    }

    private void dtpDate_ValueChanged(object sender, EventArgs e)
    {
      if (chkAutosave.Checked)
        SaveCurrent();
      else
        btnSave.Enabled = true;
    }

    private void cmbMarkGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (chkAutosave.Checked)
        SaveCurrent();
      else
        btnSave.Enabled = true;
    }

    private void chkAutosave_CheckedChanged(object sender, EventArgs e)
    {
      btnSave.Visible = !chkAutosave.Checked;
    }

    private void btnSave_EnabledChanged(object sender, EventArgs e)
    {
      chkAutosave.Enabled = !btnSave.Enabled;
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
      SaveCurrent();
      btnSave.Enabled = false;
    }

    private void markBindingSource_CurrentItemChanged(object sender, EventArgs e)
    {
      cmbMark.SelectedItem = (markBindingSource.Current as Model.Mark).MarkCode;
      cmbMarkGroup.SelectedItem = (markBindingSource.Current as Model.Mark).MarkGroup;
      btnSave.Enabled = false;
    }

    DistinctList<int> changedStudentsIDs = new DistinctList<int>();
    public DistinctList<int> GetChangetStudentsIDs ()
    {
      return changedStudentsIDs;
    }

  }
}
